import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import static java.lang.Integer.parseInt;

public class SortPoker2 {
    /**
     * 本对象是通过牌的价值进行排序
     */
    //这里是静态代码块
    static HashMap<String, Integer> hm = new HashMap<>();
    //创建集合存牌
    static ArrayList<String> list = new ArrayList<>();

    static {

        //添加不方便排序的牌
        hm.put("J", 11);
        hm.put("Q", 12);
        hm.put("K", 13);
        hm.put("A", 14);
        hm.put("2", 15);
        hm.put(" 大王", 100);
        hm.put(" 小王", 101);
        String[] color = {"♦", "♣", "♥", "♠"};
        String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        for (String n : number) {
            for (String c : color) {
                list.add(c + n);
            }
        }
    }

    //空参构造
    public SortPoker2() {
        //打乱牌序
        Collections.shuffle(list);
        //创建玩家和底牌
        ArrayList<String> lord = new ArrayList<>();
        ArrayList<String> gamer1 = new ArrayList<>();
        ArrayList<String> gamer2 = new ArrayList<>();
        ArrayList<String> gamer3 = new ArrayList<>();
        //发牌
        for (int i = 0; i < list.size(); i++) {
            if (i <= 2) {
                lord.add(list.get(i));
            } else if (i % 3 == 0) {
                gamer1.add(list.get(i));
            } else if (i % 3 == 1) {
                gamer2.add(list.get(i));
            } else {
                gamer3.add(list.get(i));
            }
        }
        sort(lord);
        sort(gamer1);
        sort(gamer2);
        sort(gamer3);
        System.out.println(lord);
        System.out.println(gamer1);
        System.out.println(gamer2);
        System.out.println(gamer3);


    }

    //排序
    public void sort(ArrayList<String> list) {
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                String color1 = o1.substring(0, 1);
                int v1 = getValue(o1);
                String color2 = o2.substring(0, 1);
                int v2 = getValue(o2);
                int i = v1 - v2;
                return i = v1 - v2 == 0 ? color1.compareTo(color2) : i;
            }
        });
    }

    //获取牌的价值
    public int getValue(String poker) {
        String str = poker.substring(1);
        boolean flag = hm.containsKey(str);
        if (flag) {
            return hm.get(str);
        } else {
            return parseInt(str);
        }

    }
}
